package com.hxd.otc.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.ClipboardManager;
import android.view.View;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.widget.BlocksToast;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 包  名 :  com.hxd.otc.utils
 * 类  名 :  AddressUtil
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/7 10:24
 * 描  述 :  地址工具类
 */
public class AddressUtil {
    //private static String PATH = SdCardUtil.getSDCardPath() + "download/";
    private static String PATH = FileUtils.getBaseFile(UIUtils.getContext()).getAbsolutePath();

    public static void copyAddress(Context context, String copy_string, String hint) {
        ClipboardManager cm;
        // 将文本内容放到系统剪贴板里。
        cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(copy_string);
        BlocksToast.get().showToast(hint);
    }

    public interface SaveAddressListener {
        void fail(String str);

        void success(String str);
    }

    public static void saveAddress(Bitmap bitmap, String suffix, SaveAddressListener listener) {
        if (SdCardUtil.isSDCardEnable()) {
            //imgAddress.buildDrawingCache(true);
            //imgAddress.buildDrawingCache();
            //Bitmap bitmap = convertViewToBitmap(imgAddress);
            if (bitmap == null) {
                if (listener != null) {
                    listener.fail(UIUtils.getContext().getString(R.string.wallet_toast_address_image_save_fail));
                }
            }
            try {

                File destDir = new File(PATH);
                if (!destDir.exists()) {
                    //L.d("MagicMirror", "Dir not exist create it " + PATH);
                    destDir.mkdirs();
                    //L.d("MagicMirror", "Make dir success: " + PATH);
                }
                /*File imageFile = new File(PATH + suffix+".jpg");
                imageFile.createNewFile();*/
                File imageFile = FileUtils.outDirectory(UIUtils.getContext(), FileUtils.DOWNLOAD);
                imageFile = new File(imageFile.getAbsoluteFile(), suffix + ".jpg");
                FileOutputStream fos = new FileOutputStream(imageFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
                //imgAddress.setDrawingCacheEnabled(false);
                if (listener != null) {
                    listener.success(imageFile.getAbsolutePath());
                    FileUtils.scanImage(BaseApplication.getInstance(), imageFile);
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (listener != null) {
                    listener.fail(UIUtils.getContext().getString(R.string.wallet_toast_address_image_save_fail));
                }
            }
        } else {
            if (listener != null) {
                listener.fail(UIUtils.getContext().getString(R.string.wallet_toast_address_sd_error));
            }
        }

    }


    public static Bitmap convertViewToBitmap(View view) {
        //view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        //view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }
}
